﻿Public Class ThuNhapForm
    Dim dt As DataTable    
    Private Sub ChiPhiForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        dt = ThuNhapDAO.GetAll()
        dgThuNhap.DataSource = dt
        dgThuNhap.Columns(1).HeaderText = "Tên loại thu nhập"
        dgThuNhap.Columns(0).Visible = False
    End Sub

    Private Sub btExit_Click(sender As System.Object, e As System.EventArgs) Handles btExit.Click
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Hide()
    End Sub

    Private Sub dgChiPhi_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgThuNhap.CellClick
        txtTen.Text = dt.Rows(dgThuNhap.CurrentRow.Index).Item("Ten").ToString()
    End Sub

    Private Sub btThem_Click(sender As System.Object, e As System.EventArgs) Handles btThem.Click
        If txtTen.Text.Trim().Length = 0 Then
            MessageBox.Show("Xin vui lòng nhập vào tên")
            Return
        End If
        Dim dr As DataRow = dt.NewRow()
        dr("Ten") = txtTen.Text
        dt.Rows.Add(dr)
        DB_Provider.Write(dt, "ThuNhap")        
    End Sub

    Private Sub btXoa_Click(sender As System.Object, e As System.EventArgs) Handles btXoa.Click
        If dgThuNhap.SelectedRows(0).Index > -1 Then
            dt.Rows(dgThuNhap.CurrentRow.Index).Delete()
            DB_Provider.Write(dt, "ThuNhap")
        End If
    End Sub

    Private Sub btSua_Click(sender As System.Object, e As System.EventArgs) Handles btSua.Click
        If txtTen.Text.Trim().Length = 0 Then
            MessageBox.Show("Xin vui lòng nhập vào tên")
            Return
        End If
        Dim index As Integer = dgThuNhap.CurrentRow.Index
        If (index > -1) Then
            dt.Rows(index).Item("Ten") = txtTen.Text
            DB_Provider.Write(dt, "ThuNhap")            
        End If
    End Sub
End Class